package com.yu.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
public class CookieAndSession {

    @RequestMapping("/cookie1")
    public void cookie1(HttpServletResponse response, HttpServletRequest request){
        Cookie cookie = new Cookie("username","luya");
        HttpSession session = request.getSession();
        System.out.println(session);
        response.addCookie(cookie);
    }

    @RequestMapping("/cookie2")
    public void cookie2(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + " = " +cookie.getValue());
        }
    }
}
